package org.despair2D.ui.core 
{
	import flash.display.Sprite;
	import org.despair2D.ui.events.VisibleEvent;
	
	import org.despair2D.core.ns_despair;
	import org.despair2D.renderer.IView;
	import org.despair2D.ui.events.InteractiveEvent;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	
	/** @inheritDoc **/
internal class SpriteAdvance extends Sprite implements IView
{
	
	/** 交互失效 **/
	ns_despair static const c_mouseDisabledFlag:uint  =  0x01
	
	/** 不可见 **/
	ns_despair static const c_invisibleFlag:uint      =  0x02
	
	/** 滤镜 **/
	ns_despair static const c_filterFlag:uint         =  0x04
	
	
	
	/** 角度 **/
	override public function get rotation():Number{ return m_rotation; }
	override public function set rotation( v:Number ) : void 
	{
		super.rotation = m_rotation = Boolean(v % 360 < 0) ? ( v % 360 + 360 ) : ( v % 360 );
	}
	
	
	/** 缩放x **/
	override public function get scaleX():Number{ return this.m_scaleX; }
	override public function set scaleX( v:Number ) : void 
	{
		super.scaleX = m_scaleX = (v < 0) ? 0 : v;
	}
	
	
	/** 缩放y **/
	override public function get scaleY():Number { return this.m_scaleY; }
	override public function set scaleY( v:Number ) : void
	{ 
		super.scaleY = m_scaleY = (v < 0) ? 0 : v;
	}

	
	/** 透明度 **/
	override public function get alpha() : Number{ return m_alpha; }
	override public function set alpha( v:Number ) : void 
	{
		super.alpha = m_alpha = v;
	}
	
	
	/** 滤镜 **/
	override public function set filters( v:Array ) : void 
	{
		super.filters = v;
		
		if (v)
		{
			m_flags |= c_filterFlag;
		}
		else
		{
			m_flags &= ~c_filterFlag;
		}
	}
	
	
	/** 是否可见 **/
	override public function get visible() : Boolean{ return !Boolean(m_flags & c_invisibleFlag) }
	override public function set visible( b:Boolean ) : void 
	{
		super.visible = b;
		
		if (b)
		{
			m_flags &= ~c_invisibleFlag;
		}
		else
		{
			m_flags |= c_invisibleFlag;
		}
		
		this.dispatchEvent(new VisibleEvent(VisibleEvent.VISIBLE_CHANGE, b))
	}
	

	/** 是否可交互 **/
	public function get interactive() : Boolean { return !Boolean(m_flags & c_mouseDisabledFlag) }
	public function set interactive( b:Boolean ) : void
	{
		if (b)
		{
			m_flags &= ~c_mouseDisabledFlag;
		}
		
		else
		{
			m_flags |= c_mouseDisabledFlag;
		}
		
		this.dispatchEvent(new InteractiveEvent(InteractiveEvent.INTERACTIVE_CHANGE, b));
	}
	
	
	
	/**
	 * 移动
	 * @param	x
	 * @param	y
	 */
	public function move( x:Number, y:Number ) : void
	{
		this.x = x;
		this.y = y;
	}	
	
	
	/**
	 * 偏移
	 * @param	tx
	 * @param	ty
	 */
	public function offset( tx:Number, ty:Number ) : void
	{
		this.x += tx;
		this.y += ty;
	}	
	
	
	/**
	 * @inheritDoc
	 */
	public function removeAllEventListeners() : void
	{
	}
	
	
	/**
	 * 回收
	 */
	ns_despair function recycle() : void
	{
		if (this.parent)
		{
			this.parent.removeChild(this);
		}
		
		if (m_flags & c_invisibleFlag)
		{
			super.visible = true;
			m_flags &= ~c_invisibleFlag;
		}
		
		if (m_flags & c_filterFlag)
		{
			super.filters = null
			m_flags &= ~c_filterFlag;
		}
		
		if (m_flags & c_mouseDisabledFlag)
		{
			m_flags &= ~c_mouseDisabledFlag;
		}
		
		if (m_alpha != 1 || m_rotation != 0 || m_scaleX != 1 || m_scaleY != 1)
		{
			this.rotation = 0;
			this.scaleX = this.scaleY = this.alpha = 1;
		}
		
		this.x = this.y = 0;
	}
	
	
	
	ns_despair var m_flags:uint
	
	ns_despair var m_alpha:Number = 1;

	ns_despair var m_scaleX:Number = 1;
	
	ns_despair var m_scaleY:Number = 1;
	
	ns_despair var m_rotation:Number = 0;
	
}

}